package com.example.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.MemberEnum;
import com.example.common.enums.RecommendEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Course;
import com.example.entity.Orders;
import com.example.exception.CustomException;
import com.example.mapper.CourseMapper;
import com.example.mapper.OrdersMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private OrdersMapper ordersMapper;

    /**
     * 增加课程
     * @param course
     */
    public void add(Course course) {
        if(RecommendEnum.YES.status.equals(course.getRecommend())){// 对课程状态做校验
            // 做一下校验,如果当前已经存在推荐课程，提示报错
            Course recommend = courseMapper.getRecommend(course.getType());
            if(ObjectUtil.isNotEmpty(recommend)){
                throw new CustomException(ResultCodeEnum.RECOMMEND_ALREADY_ERROR);
            }
        }
        course.setTime(DateUtil.format(new Date(),"yyyy-MM-dd-HH-mm-ss"));
        courseMapper.insert(course);
    }


    /**
     * 查询课程
     * @param id
     * @return
     */
    public Course selectById(Integer id){
        Course course = courseMapper.selectById(id);
        // 把敏感数据干掉
        Account currentUser = TokenUtils.getCurrentUser(); //获取当前登录用户
        Orders orders = new Orders();//存储订单
        orders.setUserId(currentUser.getId());//设置订单中用户id
        orders.setCourseId(id);//设置订单中课程id
        List<Orders> ordersList = ordersMapper.selectAll(orders);//查询是否存在相应订单
        if (ObjectUtil.isEmpty(ordersList)) {
            course.setFile("");
        }
        return course;
    }


    /**
     * 删除课程
     * @param id
     */
    public void deleteById(Integer id){
        courseMapper.deleteById(id);
    }


    /**
     * 批量删除课程
     * @param ids
     */
    public void deleteBatch(List<Integer> ids){
        for(Integer id : ids){
            courseMapper.deleteById(id);
        }
    }


    /**
     * 修改课程
     * @param course
     */
    public void updateById(Course course){
        //对课程状态做校验
        if(RecommendEnum.YES.status.equals(course.getRecommend())){
            //做一下校验,如果当前已经存在推荐课程且不是该门课程，提示报错
            Course recommend = courseMapper.getRecommend(course.getType());
            if(ObjectUtil.isNotEmpty(recommend) && !recommend.getId().equals(course.getId())){
                throw new CustomException(ResultCodeEnum.RECOMMEND_ALREADY_ERROR);
            }
        }
        courseMapper.updateById(course);
    }

    /**
     *查询所有课程
     * @param course
     * @return
     */
    public List<Course> selectAll(Course course){
        return courseMapper.selectAll(course);
    }

    /**
     * 分页查询课程
     * @param course
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Course> selectPage(Course course, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize); // 设置分页参数
        List<Course> list = courseMapper.selectAll(course); // 执行查询操作
        return PageInfo.of(list); // 查询结果封装成PageInfo对象返回
    }


    /**
     * 获取推荐课程
     * @return
     */
    public Course getRecommend(String type){
        return courseMapper.getRecommend(type);
    }

    public List<Course> selectTop8(String type){
        return courseMapper.selectTop8(type);
    }

}
